﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Toenda.TFL.Base;
using Toenda.Rails.System;
using Toenda.Rails.DataContainer;

public partial class Controls_ctrl_sendvulnerability : System.Web.UI.UserControl {
	private AccountProvider ap = new AccountProvider();
	private ContentProvider cp = new ContentProvider();

	/// <summary>
	/// Page_Load
	/// </summary>
	/// <param name="sender"></param>
	/// <param name="e"></param>
	protected void Page_Load(object sender, EventArgs e) {
		bool isLoggedIn = false;

		if(Session.Count > 0 && ap.CheckSessionExist(Session["Zappr"].ToString())) {
			isLoggedIn = true;
		}

		if(isLoggedIn) {
			Account acc = ap.GetAccountBySession(Session["Zappr"].ToString());

			txtEMail.ReadOnly = true;
			txtEMail.Text = acc.Email;
		}

		if(!IsPostBack) {
			// error handling
			if(Request.QueryString.Get("message") != null) {
				ErrorHandler handler = new ErrorHandler();
				string strErr = Request.QueryString.Get("message");
				string errCode = "";

				handler.ErrorCode = handler.Parse(strErr);
				ErrorType err = handler.ErrorCode;

				if(err != ErrorType.NoError) {
					LiteralControl litError = new LiteralControl();
					litError.Text = "<div class=\"errorBox\">";
					litError.Text += "<span class=\"txtH3\">" + handler.ErrorDescription + "</span>";
					litError.Text += "<br /><br />";

					if(err == ErrorType.DefaultError) {
						if(Request.QueryString.Get("err") != null) {
							errCode = "<br /><br />";
							errCode += GetLocalResourceObject("ASPNET").ToString() + ": ";
							errCode += Request.QueryString.Get("err").ToString();
						}

						litError.Text += "<span class=\"txtNormal\">" + handler.ErrorMessage + errCode + "</span>";
					}
					else {
						litError.Text += "<span class=\"txtNormal\">" + handler.ErrorMessage + "</span>";
					}

					litError.Text += "</div>";
					litError.Text += "<br /><br />";

					//errLogin.Controls.Add(new LiteralControl("<br /><br />"));
					errLogin.Controls.Add(litError);
				}
			}

			if(Request.QueryString.Get("send") != null) {
				string register = Request.QueryString.Get("register").ToString();

				// successfull
				if(register == "successfull") {
					LiteralControl litError = new LiteralControl();
					litError.Text = "<div class=\"successBox\">";
					litError.Text += "<span class=\"txtH3\">" + GetLocalResourceObject("successTitle").ToString() + "</span>";
					litError.Text += "<br /><br />";
					litError.Text += "<span class=\"txtNormal\">" + GetLocalResourceObject("successText").ToString() + "</span>";
					litError.Text += "</div>";
					litError.Text += "<br /><br />";

					errLogin.Controls.Add(new LiteralControl("<br /><br />"));
					errLogin.Controls.Add(litError);
				}
			}
		}
	}

	/// <summary>
	/// btnCancel_Click
	/// </summary>
	/// <param name="sender"></param>
	/// <param name="e"></param>
	protected void btnCancel_Click(object sender, EventArgs e) {
		Response.Redirect("~/default.aspx");
	}

	/// <summary>
	/// btnSave_Click
	/// </summary>
	/// <param name="sender"></param>
	/// <param name="e"></param>
	protected void btnSave_Click(object sender, EventArgs e) {
		if(txtDesc.Text.Trim() == "") {
			return;
		}

		if(txtImageID.Text.Trim() == "") {
			return;
		}

		if(txtEMail.Text.Trim() == "") {
			return;
		}

		MessageItem msg = new MessageItem();

		msg.ID = Guid.NewGuid().ToString();
		msg.ImageID = txtImageID.Text.Trim().Replace("'", "´");
		msg.Description = txtDesc.Text.Trim().Replace("'", "´");
		msg.CreateDate = DateTime.Now;
		msg.IsFinished = false;
		msg.EMail = txtEMail.Text.Trim().Replace("'", "´");

		string errMessage = "";

		try {
			ErrorType err = cp.SendVulnerabilityMessage(msg);

			if(err == ErrorType.NoError) {
				Response.Redirect("~/default.aspx?_sendvulnerability&send=successfull");
			}
			else {
				errMessage = Convert.ToString(err);
				Response.Redirect("~/default.aspx?id=_sendvulnerability&message=" + errMessage);
			}
		}
		catch(Exception ex) {
			LiteralControl litError = new LiteralControl();
			litError.Text = "<div class=\"errorBox\">";
			litError.Text += "<span class=\"txtH3\">" + GetLocalResourceObject("Error").ToString() + "</span>";
			litError.Text += "<br /><br />";
			litError.Text += "<span class=\"txtNormal\">" + ex.Message + "</span>";
			litError.Text += "</div>";
			litError.Text += "<br /><br />";

			//errLogin.Controls.Add(new LiteralControl("<br /><br />"));
			errLogin.Controls.Add(litError);
		}
	}
}
